﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MusicService
{
    public partial class MusicService : IRoleService
    {
        public bool addRole(string roleName)
        {
            bool flag = false;
            Role role = new Role();
            try
            {
                int numberRole = dc.Roles.Count() + 1;
                role.RoleID = numberRole;
                role.RoleName = roleName;
                dc.Roles.InsertOnSubmit(role);
                dc.SubmitChanges();
                flag = true;
            }
            catch (Exception e)
            {
                
            }
            return flag;
            
        }

        public bool removeRole(int roleID)
        {
            bool flag = false;
            try
            {
                Role role = dc.Roles.Single(tmp => tmp.RoleID == roleID);
                if (role != null)
                {
                    dc.Roles.DeleteOnSubmit(role);
                    dc.SubmitChanges();
                    flag = true;
                }
            }
            catch (Exception e)
            {

            }
            return flag;
        }

        public bool updateRole(int roleID, string roleName)
        {
            bool flag = false;
            try
            {
                Role role = dc.Roles.Single(tmp => tmp.RoleID == roleID);
                if (role != null)
                {
                    role.RoleName = roleName;
                    dc.SubmitChanges();
                    flag = true;
                }
            }
            catch (Exception e)
            {

            }
            return flag;
        }

        public List<Role> getAllRole()
        {
            var list = from role in dc.Roles select role;
            return list.ToList();
        }

        public Role getRoleByName(string roleName)
        {
            Role role = new Role();
            role = dc.Roles.Single(tmp => tmp.RoleName.Equals(roleName));
            return role;
        }
    }
}